// components/pickDate/pickDate.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    date: {
      type: Number,
      value: new Date().getTime(),
      observer: function (newVal, oldVal, changedPath) {
        setTimeout(() => {
          this.setCurrentDate(newVal);
        }, 100);
      }
    },
    format: {
      type: String,
      value: 'YYYY/MM/DD'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    selectedDate: '', // 选中的几月几号
    selectedWeek: '', // 选中的星期几
    curYear: 2017,  // 当前年份
    curMonth: 0,  // 当前月份
    daysCountArr: [ // 保存各个月份的长度，平年
      31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    ],
    dateList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setCurrentDate: function (timeStamp) {
      if (timeStamp > 0) {
        let _date = new Date(timeStamp);//当前时间  
        let y = _date.getFullYear();//年  
        let m = _date.getMonth() + 1;//月  
        let d = _date.getDate();//日  
        let i = _date.getDay();//星期  
        let formatted = this.getDateString(y, m, d);
        this.setData({
          show: true,
          curYear: y,
          curMonth: m,
          selectedDate: formatted,
        });
        this.getDateList(y, m - 1);
      }
    },
    getDateList: function (y, mon) {
      let vm = this;
      // 如果是否闰年，则2月是29日
      let daysCountArr = this.data.daysCountArr;
      if (y % 4 == 0 && y % 100 != 0) {
        this.data.daysCountArr[1] = 29;
        this.setData({
          daysCountArr: daysCountArr
        });
      }
      // 第几个月；下标从0开始实际月份还要再+1  
      let dateList = [];
      dateList[0] = [];
      let weekIndex = 0;//第几个星期
      for (let i = 0; i < vm.data.daysCountArr[mon]; i++) {
        let m = mon + 1;
        let d = i + 1;
        let date = new Date(y, mon, d);
        let week = date.getDay();
        dateList[weekIndex].push({
          value: {
            year: y,
            month: m,
            day: d,
            week: week,
            timeStamp: date.getTime()
          },
          formatted: this.getDateString(y, m, d)
        });
        if (week == 0) {
          weekIndex++;
          dateList[weekIndex] = [];
        }
      }
      vm.setData({
        dateList: dateList
      });
    },
    selectDate: function (e) {
      let vm = this;
      let date = e.currentTarget.dataset.date;
      vm.setData({
        selectedDate: date.formatted
      });
      this.triggerEvent('datechanged', date);
    },
    preMonth: function () {
      // 上个月
      var vm = this;
      var curYear = vm.data.curYear;
      var curMonth = vm.data.curMonth;
      curYear = curMonth - 1 ? curYear : curYear - 1;
      curMonth = curMonth - 1 ? curMonth - 1 : 12;
      vm.setData({
        curYear: curYear,
        curMonth: curMonth
      });

      vm.getDateList(curYear, curMonth - 1);
    },
    nextMonth: function () {
      // 下个月
      var vm = this;
      var curYear = vm.data.curYear;
      var curMonth = vm.data.curMonth;
      curYear = curMonth + 1 === 13 ? curYear + 1 : curYear;
      curMonth = curMonth + 1 === 13 ? 1 : curMonth + 1;
      vm.setData({
        curYear: curYear,
        curMonth: curMonth
      });

      vm.getDateList(curYear, curMonth - 1);
    },
    getDateString: function (year, month, day) {
      month = month < 10 ? ('0' + month) : month;
      day = day < 10 ? ('0' + day) : day;
      let tpl = (this.data.format || 'YYYY/MM/DD')
        .replace('YYYY', year + '')
        .replace('MM', month + '')
        .replace('DD', day + '')
      return tpl;
    }
  }
});
